package com.example.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.admin.bean.Account;
import com.example.admin.bean.User;
import com.example.admin.bean.User1;
import com.example.admin.service.AccountService;
import com.example.admin.service.UserService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Arrays;
import java.util.List;


@Controller
public class TableController {

    @Autowired
    AccountService accountService;

    @GetMapping("/basic_table")
    public String basic_table(){
        return "table/basic_table";
    }

    @GetMapping("/dynamic_table")
    public String dynamic_table(@RequestParam(value = "pn",defaultValue = "1") Integer pn,
                                Model model){
        //表格内容的遍历
        List<User1> users = Arrays.asList(new User1("zhangsan", "123456"),
                new User1("hehe", "hehe"),
                new User1("lisi", "1111"),
                new User1("haha", "haha")
        );
        model.addAttribute("users",users);

        List<Account> list = accountService.list();
        model.addAttribute("list",list);

        //分页查询
        Page<Account> accPage = new Page<>(pn, 2);
        Page<Account> page = accountService.page(accPage, null);
        model.addAttribute("page",page);
        List<Account> records = page.getRecords();
        return "table/dynamic_table";
    }

    @GetMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id,
//                         @RequestParam(value = "pn",defaultValue = "1") Integer pn,
                         RedirectAttributes ra){
        accountService.removeById(id);

//        ra.addAttribute("pn",pn);
        return "redirect/dynamic_table";
    }

    @GetMapping("/responsive_table")
    public String responsive_table(){
        return "table/responsive_table";
    }

    @GetMapping("/editable_table")
    public String editable_table(){
        return "table/editable_table";
    }
}
